//
//  RegisterViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "RegisterViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "LoginViewController.h"
#import "ContractViewController.h"
#import "registerText.h"

@interface RegisterViewController ()

@property (nonatomic,assign)BOOL agree;

@property (nonatomic,assign)int timeNum;

@property (nonatomic,strong)NSTimer *time;

@property (nonatomic,weak)registerText *textFilePhoneNum;

@property (nonatomic,weak)UIButton *buttonCheckNum;

@property (nonatomic,weak)registerText *textFileCheckNum;

@property (nonatomic,weak)registerText *textFileInviteNum;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

- (void)setupUI{
    
    self.title = @"用户注册";
    
    //手机号码
    registerText *textFilePhoneNum = [[registerText alloc]init];
    
    [self.view addSubview:textFilePhoneNum];
    //属性赋值
    self.textFilePhoneNum = textFilePhoneNum;
    
    textFilePhoneNum.placeholder = @"请输入您的手机号码";
    textFilePhoneNum.borderStyle = UITextBorderStyleRoundedRect;
    
    [textFilePhoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@300);
        make.top.equalTo(self.view).offset(114);
    }];
    
    
    //验证码
    registerText *textFileCheckNum = [[registerText alloc]init];
    
    [self.view addSubview:textFileCheckNum];
    
    //属性赋值
    self.textFileCheckNum = textFileCheckNum;
    
    textFileCheckNum.placeholder = @"请输入验证码";
    textFileCheckNum.borderStyle = UITextBorderStyleRoundedRect;
    
    [textFileCheckNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(textFilePhoneNum);
        make.width.equalTo(@180);
        make.top.equalTo(textFilePhoneNum.mas_bottom).offset(20);
    }];

    //获取验证码按钮
    UIButton *buttonCheckNum = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonCheckNum setBackgroundColor:[UIColor lightGrayColor]];
    [buttonCheckNum setTitle:@"获取验证码" forState:UIControlStateNormal];
    [buttonCheckNum setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [buttonCheckNum setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
    buttonCheckNum.enabled = NO;
    [self.view addSubview:buttonCheckNum];
    
    //属性赋值
    self.buttonCheckNum = buttonCheckNum;
    
    [buttonCheckNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(textFileCheckNum.mas_trailing).offset(20);
        make.top.equalTo(textFilePhoneNum.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];

    //获取验证码按钮添加点击事件
    [buttonCheckNum addTarget:self action:@selector(clickButtonForCheckNum:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //邀请码
    registerText *textFileInviteNum = [[registerText alloc]init];
    
    self.textFileInviteNum = textFileInviteNum;
    
    [self.view addSubview:textFileInviteNum];
    
    textFileInviteNum.placeholder = @"请输入邀请码(选填)";
    textFileInviteNum.borderStyle = UITextBorderStyleRoundedRect;
    
    
    [textFileInviteNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@300);
        make.top.equalTo(textFileCheckNum.mas_bottom).offset(20);
    }];
    
    
    //用户协议button
    UIButton *buttonAgree = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [buttonAgree setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:buttonAgree];
    
    [buttonAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.top.equalTo(textFileInviteNum.mas_bottom).offset(30);
        make.leading.equalTo(textFileInviteNum.mas_leading).offset(10);
    }];
    
    [buttonAgree addTarget:self action:@selector(clickButtonAgree:) forControlEvents:UIControlEventTouchUpInside];
    
    //本人已同意并阅读
    UILabel *labelAgree = [[UILabel alloc]init];
    labelAgree.text = @"本人已同意并阅读";
    [self.view addSubview:labelAgree];
    [labelAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(buttonAgree);
        make.leading.equalTo(buttonAgree.mas_trailing).offset(5);
    }];
    
    //用户协议
    UIButton *buttonContract = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonContract setTitle:@"用户协议" forState:UIControlStateNormal];
    [buttonContract setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:buttonContract];
    [buttonContract mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(buttonAgree);
        make.leading.equalTo(labelAgree.mas_trailing).offset(5);
    }];
    
    [buttonContract addTarget:self action:@selector(clickButtonForContract:) forControlEvents:UIControlEventTouchUpInside];
    
    //下一步按钮
    UIButton *buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonNext setTitle:@"下一步" forState:UIControlStateNormal];
    [buttonNext setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [self.view addSubview:buttonNext];
    [buttonNext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 40));
        make.centerX.equalTo(self.view);
        make.top.equalTo(buttonAgree.mas_bottom).offset(30);
    }];
    
    [buttonNext addTarget:self action:@selector(clickButtonNext:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //直接登录按钮
    UIButton *buttonLogIn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLogIn setTitle:@"已有注册用户,直接登录" forState:UIControlStateNormal];
    [buttonLogIn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:buttonLogIn];
    [buttonLogIn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-100);
    }];
    
    [buttonLogIn addTarget:self action:@selector(clickButtonToLogIncView:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark
#pragma mark - 点击获取验证码
- (void)clickButtonForCheckNum:(UIButton *)buttonCheckNum{
    
    self.timeNum = 60;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.textFilePhoneNum.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error == nil) {
            NSLog(@"发送成功");
            buttonCheckNum.enabled = NO;
            NSTimer *time = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(setGetMsgButtonTitle) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:time forMode:NSRunLoopCommonModes];
            self.time = time;
        }else{
            NSLog(@"发送失败%@",error);
        }
    }];
}

#pragma mark
#pragma mark - 定时器方法
- (void)setGetMsgButtonTitle{
    if (self.timeNum < 0) {
        [self.time invalidate];
        self.buttonCheckNum.enabled = YES;
        return;
    }
    [self.buttonCheckNum setTitle:[NSString stringWithFormat:@"%d秒",self.timeNum] forState:UIControlStateDisabled];
    [self.buttonCheckNum setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
    self.timeNum = self.timeNum - 1;
}



#pragma mark
#pragma mark - 点击同意协议
- (void)clickButtonAgree:(UIButton *)buttonAgree{
    if (self.agree) {
        
        [buttonAgree setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
        self.buttonCheckNum.enabled = NO;
        self.agree = NO;
    }else{
        
        [buttonAgree setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
        self.buttonCheckNum.enabled = YES;
        self.agree = YES;
    }
}

#pragma mark
#pragma mark - 点击直接登录
- (void)clickButtonToLogIncView:(UIButton *)button{
    LoginViewController *logInVC = [[LoginViewController alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController pushViewController:logInVC animated:YES];
}

#pragma mark
#pragma mark - 下一步按钮
- (void)clickButtonNext:(UIButton *)button{
    
    [SMSSDK commitVerificationCode:self.textFileCheckNum.text phoneNumber:self.textFilePhoneNum.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        if (error == nil) {
            NSLog(@"登录成功");
            
            NSString *userName = self.textFilePhoneNum.text;
            NSString *passWord = self.textFileCheckNum.text;
            
            [[NetWorkManager sharedManager] loginUserWihtAccount:userName password:passWord successBlock:^(Users *dto) {
                if (self.LogInCompleteBlock) {
                    //存储登录成功的标记
                    [MLDefaults setBool:YES forKey:kIsUserLoginKey];
                    self.LogInCompleteBlock(dto);
                };
                [self.navigationController popViewControllerAnimated:YES];
            } errorBlock:^(NSString *error) {
                NSLog(@"账号密码错误");
            }];
            
        }else{
            NSLog(@"登录失败");
        }
    }];

}

#pragma mark
#pragma mark - 用户协议按钮点击查看协议
- (void)clickButtonForContract:(UIButton *)button{
    
    ContractViewController *contractVC = [[ContractViewController alloc]init];
    [self.navigationController pushViewController:contractVC animated:YES];
    
}


#pragma mark
#pragma mark - 点击屏幕取消编辑模式
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.textFileCheckNum endEditing:YES];
    [self.textFilePhoneNum endEditing:YES];
    [self.textFileInviteNum endEditing:YES];
    
}




- (void)dealloc{
    
    NSLog(@"注册页面销毁");
}

@end











